<template>
  <div class="mt-30px">
    <el-form
      :model="password_form"
      :rules="password_rules"
      ref="password_ref"
      class="w-400px m-auto"
    >
      <el-form-item prop="old_pwd" label="">
        <el-input
          v-model="password_form.old_pwd"
          placeholder="旧密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="new_pwd" label="">
        <el-input
          v-model="password_form.new_pwd"
          placeholder="新密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="" label="" class="mt-40px">
        <div class="register cursor-pointer" @click="handle_password">提交</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, defineEmits } from "vue";
import { ElMessage } from "element-plus";
import { password_rules } from "../data.config";
import { change_my_password } from "@/api/index";

const password_ref = ref();
const password_form = reactive({
  old_pwd: "",
  new_pwd: "",
});

const handle_password = async () => {
  if (!password_ref.value) return;
  await password_ref.value.validate(async (valid, fields) => {
    if (valid) {
      const { data, code, message } = await change_my_password(password_form);
      if (code === 200) {
        ElMessage.success("提交成功");
        password_form.old_pwd = "";
        password_form.new_pwd = "";
      } else {
        ElMessage.error(message);
      }
    } else {
    }
  });
};
</script>

<style scoped>
.btn:hover {
  background-color: #fcf9f9;
  border-color: #fcf9f9;
  color: #bb0615;
}
.register {
  width: 514px;
  height: 34px;
  background-color: #bb0615;
  border-radius: 22px;
  font-family: MicrosoftYaHei;
  font-size: 18px;
  line-height: 34px;
  letter-spacing: 18px;
  color: #faf5f5;
  text-align: center;
}
</style>
